package com.qdxwx.web.controller.user;


import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user/course")
public class UserCourseController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserCourseController.class);

	@Autowired
	private CourseService courseService;
	@Autowired
	private CourseStudyHistoryService courseStudyHistoryService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CourseNodeService courseNodeService;

	/**
	 * 进入个人中心课程详情
	 */
	@RequestMapping("/info.html")
	public String courseInfo(HttpServletRequest request, ModelMap mmap) {
		//获取登录用户ID
		User user = LoginUser.getUser(request);
		if (user == null) {
			return VIEW_ERROR_403;
		}
		int courseId = TKConvert.toInteger(request.getParameter("id"));
		CourseDTO course = courseService.getMyCourse(user.getUserId(), courseId);
		if (course == null) {
			return VIEW_ERROR_404;
		}
		if (course.getExpireDate() != null) {
			if (course.getExpireDate().isBefore(LocalDate.now())) {
				return "redirect:/course/view/" + courseId + ".html";
			}
		}

		//我的课程学习记录
		course.setCourseStudyHistory(courseStudyHistoryService.getLastByUserId(user.getUserId(), course.getCourseId()));
		//设置学习进度
		courseStudyHistoryService.setStudyPercent(course, user.getUserId());

		//课程章节目录村
		List<CourseNode> courseNodeTree = courseNodeService.getTreeByCourseId(courseId);
		//设置章节进度
		courseStudyHistoryService.setIsStudy(courseNodeTree, user.getUserId(), courseId);
		mmap.put("courseNodeTree", courseNodeTree);

		//查询课程老师
		List<Teacher> teacherList = teacherService.getListByCourseId(courseId);
		mmap.put("teacherList", teacherList);
		mmap.put("course", course);
		/*如果是直播进入直播详情页面*/
		mmap.put("user", user);
		if ("LIVE".equals(course.getSellType())) {
			return getViewPath("ucenter/uc-live-info");
		} else {
			return getViewPath("ucenter/uc-course-info");
		}
	}

	/**
	 * 到课程评论页面
	 */
	@RequestMapping("/comment.html")
	public String courseComment(HttpServletRequest request, ModelMap mmap) {
		//获取登录用户ID
		User user = LoginUser.getUser(request);
		if (user == null) {
			return VIEW_ERROR_403;
		}
		int courseId = TKConvert.toInteger(request.getParameter("courseId"));
		Course course = courseService.get(courseId);
		if (course == null){
			return toErrorView(request, "课程不存在或已经被删除！");
		}
		mmap.put("course", course);
		mmap.put("otherId", courseId);
		mmap.put("user", user);
		return getViewPath("ucenter/uc-course-comment");
	}
}